package com.zuorenke.exception;

import org.springframework.dao.DataAccessException;
import org.springframework.remoting.RemoteAccessException;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-20 14:10:49
 **/
public class InternalException extends MyRuntimeException {
	private static final long serialVersionUID = 3908401671142156499L;
	private final String code;
	
	public InternalException(String code) {
		super("exception.internalerror", new Object[]{code});
		this.code = code;
	}
	
	public InternalException(String code, Throwable cause) {
		super("exception.internalerror", cause, new Object[]{code});
		this.code = code;
	}
	
	public String getCode() {
		return this.code;
	}
	
	public static Exception wrap(Exception e) {
		if (e instanceof DataAccessException) {
			return new InternalException("1001", e);
		} else {
			return (e instanceof RemoteAccessException ? new InternalException("1005", e) : e);
		}
	}
}